package com.njym.annotation.Impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.njym.annotation.ExistCheck;
import com.njym.entity.PlatformBusinessRec;
import com.njym.service.PlatformBusinessRecService;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author zhy
 * @date 2019-10-24 16:23
 **/

public class ExistCheckImpl implements ConstraintValidator<ExistCheck,String> {

	private PlatformBusinessRecService platformBusinessRecService;

	public ExistCheckImpl(PlatformBusinessRecService platformBusinessRecService) {
		this.platformBusinessRecService = platformBusinessRecService;
	}

	@Override
	public void initialize(ExistCheck constraintAnnotation) {

	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		Wrapper<PlatformBusinessRec> queryParams = new EntityWrapper<>();
		queryParams.eq("out_trade_no", value);
		queryParams.eq("deleted", 0);
		PlatformBusinessRec oldBusinessRec = platformBusinessRecService.selectOne(queryParams);
		return (oldBusinessRec == null);
	}
}
